package com.itcast.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.tanhua.dubbo.server.domain.Users;
import com.itcast.tanhua.dubbo.server.vo.PageInfo;
import com.mongodb.client.result.DeleteResult;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @PachageName: com.itcast.tanhua.dubbo.server.api
 * @ClassName:
 * @Description: TODO
 * @Author: Hushi
 * @DateTime: 2021/2/2210:36
 * @Version 1.0
 */
@Service(version = "1.0.0")
@Component
public class UsersApiImpl implements UsersApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public String saveUsers(Users users) {
        if (users.getUserId() == null || users.getFriendId() == null){
            return null;
        }

        Long userId = users.getUserId();
        Long friendId = users.getFriendId();

        //检查好友关系是否存在
        Query uqery = Query.query(Criteria.where("userId").is(users.getUserId()).and("friendId").is(users.getFriendId()));
        Users users1 = this.mongoTemplate.findOne(uqery, Users.class);
        if (null != users1){
            return null;
        }


        //填充好友关系并添加到数据库
        users.setId(ObjectId.get());
        users.setDate(System.currentTimeMillis());
        this.mongoTemplate.save(users);

        //添加好友和自己的关系
        Users users2 = new Users();
        users2.setDate(System.currentTimeMillis());
        users2.setFriendId(userId);
        users2.setUserId(friendId);
        users2.setId(ObjectId.get());
        this.mongoTemplate.save(users2);
        return users.getId().toHexString();
    }

    @Override
    public boolean removeUsers(Users users) {
        if (users.getUserId() == null || users.getFriendId() == null){
            return false;
        }

        Query query1 = Query.query(Criteria.where("userId").is(users.getUserId()).and("friendId").is(users.getFriendId()));
        DeleteResult remove1 = this.mongoTemplate.remove(query1, Users.class);

        Query query2 = Query.query(Criteria.where("friendId").is(users.getUserId()).and("userId").is(users.getFriendId()));
        DeleteResult remove2 = this.mongoTemplate.remove(query2, Users.class);

        return remove1.getDeletedCount() == 1 && remove2.getDeletedCount() == 1;
    }

    @Override
    public List<Users> queryAllUsersList(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return this.mongoTemplate.find(query, Users.class);
    }

    @Override
    public PageInfo<Users> queryUsersList(Long userId, Integer page, Integer pageSize){
        PageInfo<Users> pageInfo = new PageInfo<>();
        pageInfo.setTotal(0);
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageNum(page);
        Pageable pageRequest = PageRequest.of(page - 1,pageSize, Sort.by(Sort.Order.desc("created")));
        Query query = Query.query(Criteria.where("userId").is(userId)).with(pageRequest);
        List<Users> usersList = this.mongoTemplate.find(query, Users.class);
        if (CollectionUtils.isEmpty(usersList)){
            return pageInfo;
        }
        pageInfo.setRecords(usersList);
        return pageInfo;
    }
}
